<?php

namespace app\api\controller\v1;

use app\common\controller\ApiCommon;
use app\common\model\Building;
use app\common\model\Assistant;
use app\common\model\DutyRecord;
use app\common\model\DutyArrange;
use app\common\model\SystemConfig;

class DutySignInOut extends ApiCommon
{
    use \app\common\model\TimeCommon;
    public function index()
    {
        $this->checkPermission(1);
        return parent::index();
    }


    /**
     * 自助签到
     * 参数有:akey,bkey,force
     * @return void
     */
    public function signInSelf()
    {
        $request = $this->request;
        $akeyY = $request->param('akey');  //助理密钥
        $bkeyY = $request->param('bkey');  //签到点密钥

        //检查是否传入akey
        if (!$akeyY) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入akey');
        }

        //检查是否传入bkey
        if (!$bkeyY) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入bkey');
        }

        //解码bkey并进行检查
        if (!($bkey = Building::decodeKey($bkeyY, model('SystemConfig')->getValueInt('BKeyExpire')))) {
            return json_response([], STATUS_FAIL, "很抱歉,您的签到地点的二维码已失效,请重新签到");
        }

        //检查bkey是否已经绑定
        if (!($building = Building::where('key', $bkey)->find())) {
            return json_response([], STATUS_FAIL, "很抱歉,您的签到二维码未绑定值班地点,请联系超级管理员进行绑定");
        }

        //检查akey是否有效,并获取对象
        if (!($assistant =  Assistant::getByAKey($akeyY))) {
            return json_response([], STATUS_FAIL, "很抱歉,此签到链接已经失效,请重新获取签到链接");
        }
        $force= $request->param('force');

        $date = model('CourseAdjust')->getDate();
        if ($date===false){
            if ($force){
                $date=date('Y-m-d');
            }
        }

        //获取当前的时间
        $timeDeltail = $this->getTimeDetail($date);

        //检查是否在值班时间内
        $data = [
            'term_id' => $timeDeltail['term']->id,
            'building' => $building->getAttr('name'),
            'assistant_id' => $assistant->id,
            'date' => date('Y-m-d')
        ];
        if (($duty = DutyRecord::where($data)->whereNotNull('start_time')->whereNull('end_time')->find())) {
            return json_response([$duty->start_time], STATUS_FAIL, "您已经签到,签到时间:" . $duty->start_time);
        }

        if (!DutyArrange::checkDuty($data['assistant_id'], $data['building'], $data['term_id'], $timeDeltail['week'], $timeDeltail['class_week'])) {
            if ($force) {
                $data['manual'] = 1;
            } else {
                return json_response(['forbidden'], STATUS_FAIL, "当前不在值班时间内,如有特殊情况,请勾选强制签到".$request->param('force'));
            }
        }
        $data['start_time'] = date('H:i:s');
        if ($record =  DutyRecord::create($data)) {
            return json_response([['building' => $building->getAttr('name'), 'start_time' => $record->start_time]], STATUS_OK, '在"' . $building->getAttr('name') . '"于"' . $record->date . ' ' . $record->start_time . '"签到成功');
        } else {
            return json_response([], STATUS_FAIL, "签到失败,原因不明");
        }
    }

    /**
     * 签退接口
     *
     * @return void
     */
    public function signOutSelf()
    {
        $request = $this->request;
        $akeyY = $request->param('akey');  //助理密钥
        $bkeyY = $request->param('bkey');  //签到点密钥

        //检查是否传入akey
        if (!$akeyY) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入akey');
        }

        //检查是否传入bkey
        if (!$bkeyY) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入bkey');
        }

        //解码bkey并进行检查
        if (!($bkey = Building::decodeKey($bkeyY, model('SystemConfig')->getValueInt('BKeyExpire')))) {
            return json_response([], STATUS_FAIL, "很抱歉,您的签到地点的二维码已失效,请重新签到");
        }

        //检查bkey是否已经绑定
        if (!($building = Building::where('key', $bkey)->find())) {
            return json_response([], STATUS_FAIL, "很抱歉,您的签到二维码未绑定值班地点,请联系超级管理员进行绑定");
        }

        //检查akey是否有效,并获取对象
        if (!($assistant =  Assistant::getByAKey($akeyY))) {
            return json_response([], STATUS_FAIL, "很抱歉,此签到链接已经失效,请重新获取签到链接");
        }

        //获取当前的时间
        $timeDeltail = $this->getTimeDetail(model('CourseAdjust')->getDate());

        $data = [
            'term_id' => $timeDeltail['term']->id,
            'building' => $building->getAttr('name'),
            'assistant_id' => $assistant->id,
            'date' => date('Y-m-d')
        ];
        if (!($record = DutyRecord::where($data)->whereNotNull('start_time')->whereNull('end_time')->find())) {
            return json_response([], STATUS_FAIL, "您当前没有待完成的签退");
        }
        $record->end_time = date('H:i:s');

        if ($record->save()) {
            return json_response([], STATUS_OK, '在"' . $building->getAttr('name') . '"于"' . $record->date . ' ' . $record->end_time . '"签退成功');
        } else {
            return json_response([], STATUS_FAIL, "签退失败,原因不明");
        }
    }

    /**
     * 检查BKey是否已经绑定的接口
     *
     * @return void
     */
    public function checkBKeyValidate(){
        if (!($bkey=$this->request->param('bkey'))){
            return json_response([],STATUS_INVALD_PARAM,"必须传入有效的bkey");
        }
       
       if (!($bkey= Building::decodeKey($bkey,model('SystemConfig')->getValueInt('BKeyExpire'))))
        return json_response([],STATUS_INVALD_PARAM,"传入的BKey无效");
        if (Building::where('key',$bkey)->find()){
            return json_response([]);
        } else {
            return json_response([],STATUS_FAIL,"该教学楼信息未绑定或无效");
        }
    }
}
